#!/usr/bin/perl -w
package SimpleError;
use warnings;
use strict;
use Exporter;
use Carp ();
our @ISA = qw(Exporter);
our @EXPORT = qw( error message message_f warning );

sub error {
    my $exit_status = -1;
    my ($package, $filename, $line, $subroutine) = get_context();
    print STDERR "----Error.Occured----\n";
    print STDERR '!', "$subroutine: ", @_, "\n";
    print STDERR ' ', "from: $filename +$line\n";
    #print STDERR "\a"; # beep
    Carp::confess("----Stack.Trace---\n");
    #exit $exit_status;
}

sub message {
    my ($package, $filename, $line, $subroutine) = get_context();
    print STDERR '-', "$subroutine: ", @_, "\n";
    return 1;
}

sub message_f {
    my $format = shift;
    my ($package, $filename, $line, $subroutine) = get_context();
    print STDERR '-', "$subroutine: ", sprintf($format, @_), "\n";
    return 1;
}

sub warning {
    my ($package, $filename, $line, $subroutine) = get_context();
    print STDERR '?', "$subroutine: ", @_, "\n";
    return 0;
}

sub debug {
    my ($package, $filename, $line, $subroutine) = get_context();
    print STDERR '*', "$subroutine: ", @_, "\n";
    return 0;
}

sub assert {
    my ($package, $filename, $line, $subroutine) = get_context();
    die("$subroutine: assertion failure: $_[0]\n(from: $filename +$line)\n")
        unless eval($_[0]);
    1;
}

sub get_context {
    my ($filename, $line) = (caller(1))[1, 2];
    my ($package, $subroutine) = (caller(2))[0, 3];
    $subroutine = "main" unless defined($subroutine);
    return ($package, $filename, $line, $subroutine);
}

1;
